package com.ssy.lingxi.transaction.controller.pc;


import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dataauth.annotation.common.DataAuth;
import com.ssy.lingxi.transaction.api.dto.request.*;
import com.ssy.lingxi.transaction.api.dto.response.*;
import com.ssy.lingxi.transaction.api.enums.InquiryExternalStatusEnum;
import com.ssy.lingxi.transaction.api.enums.InquiryInteriorStatusEnum;
import com.ssy.lingxi.transaction.entity.record.InquiryExternalRecord;
import com.ssy.lingxi.transaction.entity.record.InquiryInteriorRecord;
import com.ssy.lingxi.transaction.service.IInquiryService;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @menu: 交易能力-商品询价
 * @author wrc
 * @date 2020/8/5
 */
@RestController
@RequestMapping("/transaction")
public class InquiryController extends BaseController {

    @Resource
    IInquiryService inquiryService;

    /**
     * 商品询价-询价单列表查询
     */
    @DataAuth(tableName = "trade_inquiry", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "user_id")
    @GetMapping("/inquiry/add/list")
    public Wrapper<PageData<InquiryListResponse>> inquiryAddList(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryAddList(request, sysUser);
    }

    /**
     * 商品询价-待新增询价单列表查询
     */
    @DataAuth(tableName = "trade_inquiry", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "user_id")
    @GetMapping("/inquiry/list")
    public Wrapper<PageData<InquiryListResponse>> inquiryList(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryList(request, sysUser);
    }

    /**
     * 商品询价-询价单详情
     */
    @GetMapping("/inquiry/list/details")
    public Wrapper<InquiryListDetailsResponse> inquiryListDetails(InquiryListDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryListDetails(request,sysUser);
    }

    /**
     * 商品询价-新增询价单
     */
    @PostMapping("/inquiry/list/add")
    public Wrapper<Long> inquiryListAdd(@Valid @RequestBody InquiryListAddRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryListAdd(request, sysUser);
    }

    /**
     * 商品询价-删除询价单
     */
    @PostMapping("/inquiry/list/delete")
    public Wrapper inquiryListDelete(@Valid @RequestBody InquiryListDetailsRequest request) {
        return inquiryService.inquiryListDelete(request);
    }

    /**
     * 商品询价-批量删除询价单
     */
    @PostMapping("/inquiry/list/delete/all")
    public Wrapper inquiryListDeleteAll(@Valid @RequestBody InquiryListDeleteAllRequest request) {
        return inquiryService.inquiryListDeleteAll(request);
    }

    /**
     * 商品询价-修改询价单
     */
    @PostMapping("/inquiry/list/update")
    public Wrapper inquiryListUpdate(@Valid @RequestBody InquiryListUpdateRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryListUpdate(request, sysUser);
    }

    /**
     * 商品询价-询价商品列表查询
     */
    @GetMapping("/enquiry/product/list")
    public Wrapper<PageData<InquiryListProductResponse>> enquiryProductList(EnquiryProductListRequest request) {
        return inquiryService.enquiryProductList(request);
    }

    /**
     * 商品询价-询价商品列表查询
     */
    @GetMapping("/enquiry/product/all")
    public Wrapper<List<InquiryListProductResponse>> enquiryProductAll(EnquiryProductListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.enquiryProductAll(request, sysUser);
    }

    /**
     * 商品询价-提交审核
     */
    @PostMapping("/inquiry/list/submit")
    public Wrapper inquiryListSubmit(@RequestBody InquiryListSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryListSubmit(request, sysUser);
    }

    /**
     * 商品询价-批量提交审核
     */
    @PostMapping("/inquiry/list/submit/all")
    public Wrapper inquiryListSubmitAll(@RequestBody InquiryListSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryListSubmitAll(request, sysUser);
    }

    /**
     * 商品询价-待审核询价单一级列表
     */
    @DataAuth(tableName = "trade_inquiry", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "user_id")
    @GetMapping("/inquiry/to/audit/list")
    public Wrapper<PageData<InquiryListResponse>> inquiryToAuditList(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryToAuditList(request, sysUser);
    }

    /**
     * 商品询价-询价单审核（一级）
     */
    @PostMapping("/inquiry/documents/review")
    public Wrapper inquiryDocumentsReview(@RequestBody InquiryListSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryDocumentsReview(request, sysUser);
    }

    /**
     * 商品询价-批量审核询价单审核（一级）
     */
    @PostMapping("/inquiry/documents/review/all")
    public Wrapper inquiryDocumentsReviewAll(@RequestBody InquiryListSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryDocumentsReviewAll(request, sysUser);
    }

    /**
     * 商品询价-待审核询价单二级列表
     */
    @DataAuth(tableName = "trade_inquiry", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "user_id")
    @GetMapping("/inquiry/to/audit/list/two")
    public Wrapper<PageData<InquiryListResponse>> inquiryToAuditListTwo(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryToAuditListTwo(request, sysUser);
    }

    /**
     * 商品询价-审核询价单（二级）
     */
    @PostMapping("/inquiry/documents/review/two")
    public Wrapper inquiryDocumentsReviewTwo(@RequestBody InquiryListSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryDocumentsReviewTwo(request, sysUser);
    }

    /**
     * 商品询价-批量审核询价单（二级）
     */
    @PostMapping("/inquiry/documents/review/all/two")
    public Wrapper inquiryDocumentsReviewAllTwo(@RequestBody InquiryListSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryDocumentsReviewAllTwo(request, sysUser);
    }

    /**
     * 商品询价-待提交询价单列表
     */
    @DataAuth(tableName = "trade_inquiry", memberIdColumnName = "create_member_id", roleIdColumnName = "create_role_id", userIdColumnName = "user_id")
    @GetMapping("/submit/inquiry/sheet/list")
    public Wrapper<PageData<InquiryListResponse>> submitInquirySheetList(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.submitInquirySheetList(request, sysUser);
    }

    /**
     * 商品询价-提交询价单
     */
    @PostMapping("/inquiry/submit")
    public Wrapper inquirySubmit(@RequestBody InquiryListSubmitRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquirySubmit(request, sysUser);
    }

    /**
     * 商品询价-批量提交询价单
     */
    @PostMapping("/inquiry/submit/all")
    public Wrapper inquirySubmitAll(@RequestBody InquiryListSubmitAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquirySubmitAll(request, sysUser);
    }

    /**
     * 商品询价-查询上游供应会员和商品
     */
    @GetMapping("/inquiry/getUpperMemberCommodity")
    public Wrapper<InquiryUpperMemberCommodityResponse> getUpperMemberCommodity(@RequestParam("inquiryId") Long inquiryId) {
        return inquiryService.getUpperMemberCommodity(inquiryId);
    }

    /**
     * 商品询价-内部工作流程枚举
     */
    @GetMapping("/product/inquiry/interior/state/enum")
    private Wrapper<List<QuotationExternalStateEnumResponse>> getProductInquiryInteriorStateEnum() {
        return Wrapper.success(Arrays.stream(InquiryInteriorStatusEnum.values()).map(r -> {
            QuotationExternalStateEnumResponse status = new QuotationExternalStateEnumResponse();
            status.setState(r.getState());
            status.setName(r.getName());
            return status;
        }).collect(Collectors.toList()));
    }

    /**
     * 商品询价-外部工作流程枚举
     */
    @GetMapping("/product/inquiry/external/state/enum")
    private Wrapper<List<QuotationExternalStateEnumResponse>> getProductInquiryExternalStateEnum() {
        return Wrapper.success(Arrays.stream(InquiryExternalStatusEnum.values()).map(r -> {
            QuotationExternalStateEnumResponse status = new QuotationExternalStateEnumResponse();
            status.setState(r.getState());
            status.setName(r.getName());
            return status;
        }).collect(Collectors.toList()));
    }

    /**
     * 商品询价-用户积分数据同步处理内部接口
     */
    @PostMapping("/member/inquiry/integral/update")
    private Wrapper memberInquiryIntegralUpdate(@Valid @RequestBody MemberInquiryIntegralUpdateRequest request) {
        return inquiryService.memberInquiryIntegralUpdate(request);
    }

    /**
     * 商城能力--企业商城-商品询价动态
     * @param shopId 商城id
     */
    @GetMapping("/inquiry/getShopInquiryList")
    public Wrapper<PageData<ShopInquiryListResponse>> getShopInquiryList(PageVO pageVO, @RequestParam("shopId") Long shopId) {
        return inquiryService.getShopInquiryList(pageVO, shopId);
    }

    /**
     * 商城能力--企业商城&渠道商城共用--店铺-询价商品详情-接口
     */
    @GetMapping("/inquiry/product/details")
    public Wrapper<InquiryListProductResponse> inquiryProductDetails(@Validated  BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.inquiryProductDetails(request,sysUser);
    }

    /**
     * 商品询价--内部流转记录--操作日志
     * @param inquiryListId 商品询价单ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @RequestMapping(value = "/getInquiryInteriorRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<InteriorInquiryListLogResponse>> getInquiryInteriorRecordList(PageVO pageVO, @RequestParam(value = "inquiryListId", required = false) Long inquiryListId, @RequestParam(value = "startTime", required = false) Long startTime, @RequestParam(value = "endTime", required = false) Long endTime) {
        Page<InquiryInteriorRecord> result = inquiryService.getInquiryInteriorRecordList(pageVO, inquiryListId, startTime, endTime);
        List<InteriorInquiryListLogResponse> tenderOutCheckRecordList = result.getContent().stream().map(tenderOutCheckRecord -> BeanUtil.copyProperties(tenderOutCheckRecord, InteriorInquiryListLogResponse.class)).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), tenderOutCheckRecordList));
    }

    /**
     * 商品询价--外部流转记录--操作日志
     * @param inquiryListId 商品询价单ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @RequestMapping(value = "/getInquiryExternalRecordList", method = RequestMethod.GET)
    public Wrapper<PageData<ExternalInquiryListLogResponse>> getInquiryExternalRecordList(PageVO pageVO, @RequestParam(value = "inquiryListId", required = false) Long inquiryListId, @RequestParam(value = "startTime", required = false) Long startTime, @RequestParam(value = "endTime", required = false) Long endTime) {
        Page<InquiryExternalRecord> result = inquiryService.getInquiryExternalRecordList(pageVO, inquiryListId, startTime, endTime);
        List<ExternalInquiryListLogResponse> tenderOutCheckRecordList = result.getContent().stream().map(tenderOutCheckRecord -> BeanUtil.copyProperties(tenderOutCheckRecord, ExternalInquiryListLogResponse.class)).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), tenderOutCheckRecordList));
    }
}
